using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

namespace Thinxoft.HttpLib
{
	/// <summary>
	/// Summary description for MimeTypes.
	/// </summary>
	public class MimeTypes : MarshalByRefObject
	{
		XmlNode  mimeTypes;
       
        public MimeTypes(XmlNode mimeTypes)
		{
            this.mimeTypes = mimeTypes;          
		}

		public void AddMimeType(string fileExtention, string mimeType)
		{
            XmlDocument temp = new XmlDocument();
            temp.LoadXml(string.Format("<temp><mime name=\"{0}\">{1}</mime></temp>",fileExtention,mimeType));
            XmlNode mime = mimeTypes.OwnerDocument.ImportNode(temp.DocumentElement["mime"], true);
            mimeTypes.AppendChild(mime);
          
		}

		public void RemoveMimeType(string fileExtention)
		{
            bool found;
            XmlNode mime = Find(fileExtention,out found);
            if (found)
            {
                mimeTypes.RemoveChild(mime);
            }
        }

		public string FindMimeTypeForFileExtention(string fileExtention)
		{
            bool found;
            XmlNode mime = Find(fileExtention, out found);
            if (found)
            {
                return mime.InnerText;
            }
            else
                return "*/*";           
		}

        public XmlNode Find(string extension, out bool found)
        {
            XmlNode ret = null;
            found = false;
            foreach (XmlNode mime in mimeTypes)
            {
                if (mime.Attributes["name"].Value == extension)
                {
                    found = true;
                    ret = mime;
                    return ret;
                }
            }
            return ret;
        }
    }
}
